package com.example.hp.fragmenttest_321lz.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hp.fragmenttest_321lz.R;
import com.example.hp.fragmenttest_321lz.adapter.MyAdapter;
import com.example.hp.fragmenttest_321lz.bean.ArticleBean;
import com.example.hp.fragmenttest_321lz.iface.ArticleListener;
import com.example.hp.fragmenttest_321lz.model.ArticleModel;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment4 extends Fragment {

    private TextView tvinfo;
    private View view = null;
    private Context context;

    //1copy by mainactivity
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    // private MyAdapter mAdapter;
    private MyAdapter myAdapter;
    private List<ArticleBean> list = null;
    private ArticleListener articleListener = new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> list) {
            list = list;
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }
    };

    @Nullable

    private void initRecycleView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView4);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        myAdapter = new MyAdapter(context);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        //动态加载Fragment1的布局文件
        view = inflater.inflate(R.layout.fragment4, container, false);

        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载Fragment设置", Toast.LENGTH_SHORT).show();
        initRecycleView();
        ArticleModel articleModel = new ArticleModel();
        articleModel.getResultList("tware", 2, "rv1l2tidbomann2qn86nln6ur2", articleListener);
        //返回动态生成的view
        return view;
    }
}